package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.DirectEvaluation;
import org.zwy.gradetrack.event.AchievementCalculationEvent;
import org.zwy.gradetrack.service.DirectEvaluationService;
import org.zwy.gradetrack.vo.DirectEvaluationVO;

@RestController
@RequestMapping("/api/direct-evaluations")
public class DirectEvaluationController {

    @Autowired
    private DirectEvaluationService directEvaluationService;

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    /**
     * 分页查询直接评价
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER', 'GRADUATE')")
    public ResponseEntity<Result<Page<DirectEvaluationVO>>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            DirectEvaluation evaluation) {
        Page<DirectEvaluation> page = new Page<>(current, size);
        return ResponseEntity.ok(Result.success(directEvaluationService.pageWithCondition(page, evaluation)));
    }

    /**
     * 获取直接评价详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER', 'GRADUATE')")
    public ResponseEntity<Result<DirectEvaluation>> getById(@PathVariable Integer id) {
        return ResponseEntity.ok(Result.success(directEvaluationService.getById(id)));
    }

    /**
     * 创建直接评价
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<DirectEvaluation>> create(@RequestBody DirectEvaluation evaluation) {
        // 计算评价指标
        directEvaluationService.calculateEvaluationMetrics(evaluation);
        // 保存评价
        directEvaluationService.save(evaluation);
        // 发布达成度计算事件
        eventPublisher.publishEvent(new AchievementCalculationEvent(this, 
            evaluation.getIndicatorId().longValue(), 
            evaluation.getAcademicYear()));
        return ResponseEntity.ok(Result.success(evaluation));
    }

    /**
     * 更新直接评价
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<DirectEvaluation>> update(
            @PathVariable Integer id,
            @RequestBody DirectEvaluation evaluation) {
        evaluation.setId(id);
        // 计算评价指标
        directEvaluationService.calculateEvaluationMetrics(evaluation);
        // 更新评价
        directEvaluationService.updateById(evaluation);
        // 发布达成度计算事件
        eventPublisher.publishEvent(new AchievementCalculationEvent(this, 
            evaluation.getIndicatorId().longValue(), 
            evaluation.getAcademicYear()));
        return ResponseEntity.ok(Result.success(evaluation));
    }

    /**
     * 删除直接评价
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public ResponseEntity<Result<Void>> delete(@PathVariable Integer id) {
        directEvaluationService.removeById(id);
        return ResponseEntity.ok(Result.success());
    }

    /**
     * 根据课程ID和指标点ID获取评价列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER', 'GRADUATE')")
    public ResponseEntity<Result<java.util.List<DirectEvaluation>>> list(
            @RequestParam Integer courseId,
            @RequestParam Integer indicatorId) {
        return ResponseEntity.ok(Result.success(
            directEvaluationService.getByCourseAndIndicator(courseId, indicatorId)
        ));
    }
}